import { useSettingStore } from '@/store/modules/setting'
import { LIVE_ROUTER_BLACK_LIST } from '@/config/live-icon-https'
import { useUserStore } from '@/store/modules/user'
import type { LinkType } from '@decoration/components/types'
import { TOKEN_DEL_KEY } from '@/utils/tokenConfig'
import { doDistribution, doShopInfo } from '@/apis/shops'
export default function linkNavTo(link: LinkType) {
  if (typeof link.type !== 'number') return
  switch (link.type) {
    case 0:
      functionNavMethod(link)
      break
    case 1:
      commodityNavMethod(link)
      break
    case 2:
      activityNavMethod(link)
      break
    case 3:
      cusPageNavMethod(link)
      break
    case 6:
      cusLinkNavMethod(link)
      break
    default:
      miniAppNavMethod(link)
      break
  }
}

/**
 * 功能页跳转处理方法
 */
async function functionNavMethod(link: LinkType) {
  // 平台首页跳转
  if (link.url === '/') {
    useSettingStore().NAV_TO_INDEX(link.name)
  } else if (link.url === '/basePackage/pages/merchant/Index') {
    // 店铺首页跳转
    uni.navigateTo({
      url: `/basePackage/pages/merchant/Index?shopId=${link.shopId}&tabId=${link.id}`,
    })
  } else {
    const $userStore = useUserStore()
    if (LIVE_ROUTER_BLACK_LIST.includes(link.url) && !$userStore.getterToken) {
      // 跳转直播 需登录检验身份
      // uni.showToast({
      //     icon: 'none',
      //     title: '请先登录',
      // })
      uni.$emit(TOKEN_DEL_KEY)
      return
    }
    const type = link.name === '供应商申请' ? 'SUPPLIER' : link.name === '商户申请' ? 'COMMON' : ''
    const { code, data } = await doShopInfo(type)
    if (link.name === '供应商申请' && data && code) {
      uni.navigateTo({
        url: '/basePackage/pages/applyFournisseurs/toExamine',
      })
    } else if (link.name === '商户申请' && data && code) {
      uni.navigateTo({
        url: '/basePackage/pages/applyMerchant/toExamine',
      })
    } else {
      uni.navigateTo({
        url: link.url,
        fail: () => {
          uni.showToast({
            icon: 'none',
            title: '该路径不存在',
          })
        },
      })
    }
  }
}
/**
 * 商品详情跳转
 */
function commodityNavMethod(link: LinkType) {
  uni.navigateTo({
    url: `${link.url}?goodId=${link.id}&shopId=${link.shopId}`,
  })
}
/**
 * 活动营销跳转
 */
async function activityNavMethod(link: LinkType) {
  const { code, data } = await doDistribution()
  if (data && data.status !== 'NOT_APPLIED' && code && link.name === '分销中心') {
    if (data.visited) {
      uni.navigateTo({
        url: link.url,
        fail: () => {
          uni.showToast({
            icon: 'none',
            title: '该路径不存在',
          })
        },
      })
    } else {
      uni.navigateTo({
        url: '/pluginPackage/distribute/toExamine',
      })
    }
  } else {
    uni.navigateTo({
      url: link.url,
      fail: () => {
        uni.showToast({
          icon: 'none',
          title: '该路径不存在',
        })
      },
    })
  }
}
/**
 * 自定义页面
 */
function cusPageNavMethod(link: LinkType) {
  if (!link.shopId) {
    // 平台自定义页面
    uni.navigateTo({
      url: `/basePackage/pages/customPage/CustomPagePlatform?id=${link.append}&shopId=${link.shopId || 0}&pageName=${link.name}`,
    })
  } else {
    uni.navigateTo({
      url: `/basePackage/pages/customPage/CustomPageShop?id=${link.append}&shopId=${link.shopId}&pageName=${link.name}`,
    })
  }
}
/**
 * 自定义链接跳转(外部链接)
 */
function cusLinkNavMethod(link: LinkType) {
  // #ifndef H5
  uni.navigateTo({
    url: `/basePackage/pages/webView/WebView?url=${link.url}`,
  })
  // #endif
  // #ifdef H5
  window.location.href = link.url
  // #endif
}
/**
 * 跳转小程序方法
 */
function miniAppNavMethod(link: LinkType) {
  try {
    uni.navigateToMiniProgram({
      appId: link.url,
      envVersion: 'release',
    })
  } catch {
    uni.showToast({ title: 'h5暂不支持跳转小程序', icon: 'none' })
    return
  }
}
